<?php
/**
 * Created by PhpStorm.
 * User: m1514
 * Date: 2016/12/29
 * Time: 16:12
 */

namespace Admin\Controller;


use Common\Model\BrandLogoModel;
use Common\Model\ClassModel;
use Common\Model\CommodityAttrModel;
use Common\Model\CommodityModel;
use Common\Model\GenrdAttrModel;

class CommodityController extends LoginController
{
    //展示页
    public function index()
    {

        //获取商品表和货品表关联数据
        $Commodity = (new CommodityModel())->select();
//        ->alias('c')->join("shop_goods_list g on g.glid=c.gid")
//        dd($Commodity);die;
        //分配
        $this->assign('Commodity', $Commodity);
        //显示模板
        $this->display();
    }

    //添加
    public function add()
    {
        //1获取所属分类
        $categoryData = (new \Org\Util\DataTree())->tree((new ClassModel())->select(), 'cname');
        $this->assign('categoryData', $categoryData);
        //2获取所属品牌
        $brandData = (new BrandLogoModel())->select();
        $this->assign('brandData', $brandData);
        //是否为post提交过来的数据
        if (IS_POST) {

            $CommodityModel = new CommodityModel();
            $res = $CommodityModel->addshop();
            if ($res['status'] == 'success') {

                $this->success($res['message'], u('index'));
                die;
            } else {
                $this->error($res['message']);
                die;
            }
        }
        //显示模板
        $this->display();
    }

    //异步获取规格和属性数据
    public function ajaxGetAttr()
    {
        if (IS_AJAX) {
            $tid = I('post.tid');
            //获取tid在类型属性表对应的所有数据
            $type_arrtData = (new GenrdAttrModel())->where("tid={$tid}")->select();
            //将属性值tavalue转为数组
            foreach ($type_arrtData as $k => $v) {
                $type_arrtData[$k]['tavalue'] = explode(',', $v['tavalue']);
            }
//            dd($type_arrtData);die;
            //框架提供给我们的ajax返回
            $this->ajaxReturn($type_arrtData);
        } else {
            $this->error('非法请求');
        }
    }

    //异步上传图片
    public function ajaxUpload()
    {
        //调用框架上传类来处理
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        //current  获取兼键值，在这转为一维数组
        $info = current($upload->upload());
        $path = 'Uploads/' . $info['savepath'] . $info['savename'];
        echo $path;
        exit;
    }

    //异步删除图片
    public function ajaxDelImg()
    {
        if (IS_AJAX) {
            $path = $_POST['path'];
            if (file_exists($path)) {
                unlink($path);
            }
        }
    }

    //编辑
    public function edit()
    {
        $gid = I('get.gid');
        // dd($gid);die;
        //获取所有分类数据
        $categoryData = (new \Org\Util\DataTree())->tree((new ClassModel())->select(), 'cname');
        $this->assign('categoryData', $categoryData);
//        dd($categoryData);
        // 获取品牌列表
        $brandData = (new BrandLogoModel())->select();
//        dd($brandData);
        $this->assign('brandData', $brandData);
        //获取旧数据
        $editData = (new CommodityModel())->alias('c')->join('shop_detail d on c.gid=d.gid')->where("c.gid={$gid}")->find();
        $this->assign('editData', $editData);
        //将big字段的大图处理成数组
        $editData['big'] = explode(',', $editData['big']);
        dd($editData);
        //根据tid获取属性
        $goodsAttrData = (new GenrdAttrModel())->where("tid={$editData['tid']} and class=0")->select();
        foreach ($goodsAttrData as $k => $v) {
            $goodsAttrData[$k]['tavalue'] = explode(',', $v['tavalue']);
        }
        $this->assign('goomdsAttrData', $goodsAttrData);
        //在商品属性表中查看添加了那些属性
        $goods_attr = (new CommodityAttrModel())->where("gid={$gid}")->getField('gtid,gtvalue');
        $this->assign('goods_attr', $goods_attr);
        //在商品属性表中查看添加了那些属性
        $TypeData = (new GenrdAttrModel())->alias('ta')->join('shop_goods_attr ga on ta.taid=ga.taid')->where("ga.gid={$gid} and ta.class=1")->select();
        foreach ($TypeData as $k => $v) {
            $TypeData[$k]['tavalue'] = explode(',', $v['tavalue']);
        }
//        dd($TypeData);
        $this->assign('TypeData', $TypeData);
        //执行编辑操作
        if (IS_POST) {
//            dd(I('post.'));die;
            $CommodityModel = new CommodityModel();
            $rese = $CommodityModel->edit();
            if ($rese['status'] == 'success') {
                $this->success($rese['message'], u('index'));
                die;
            } else {
                $this->error($rese['message']);
                die;
            }
        }
        //显示模板
        $this->display();
    }

    //商品删除
    public function del()
    {
        $delData = (new CommodityModel());
        $resd = $delData->del();
        if ($resd['status'] == 'success') {

            $this->success($resd['message'], u('index'));
            die;
        } else {
            $this->error($resd['message']);
            die;
        }

    }
}